package by.gravity.grsu.shedule;

import java.util.Calendar;
import java.util.Date;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import by.gravity.common.utils.ProgressUtil;
import by.gravity.grsu.shedule.adapter.WeekPagerAdapter;
import by.gravity.grsu.shedule.fragments.SelectWeekFragment;
import by.gravity.grsu.shedule.model.Week;
import by.gravity.grsu.shedule.provider.SheduleProvider;
import by.gravity.grsu.shedule.provider.listener.OnLoadWeekListener;
import by.gravity.grsu.shedule.util.CalendarHelper;

import com.viewpagerindicator.PageIndicator;

public class MainActivity extends FragmentActivity {

	private static final String TAG = MainActivity.class.getSimpleName();

	private SheduleProvider mProvider;

	private ViewPager mPager;

	private PageIndicator mIndicator;

	private ProgressDialog mProgressDialog;

	private Date mCurrentWeek;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.a_main);
		mPager = (ViewPager) findViewById(R.id.pager);
		mIndicator = (PageIndicator) findViewById(R.id.indicator);
		mProvider = new SheduleProvider(this);
		mProgressDialog = new ProgressDialog(this);
		mProgressDialog.setTitle(getString(R.string.progress_title));
		mProgressDialog.setMessage(getString(R.string.progress_msg));
		initFragments();
	}

	private void initFragments() {
		if (mCurrentWeek != null) {
			initFragments(mCurrentWeek);
		} else {
			initFragments(CalendarHelper.getStartOfWeekDate());
		}

	}

	private void initFragments(Date date) {
		ProgressUtil.showLoading(mProgressDialog, this);

		mProvider.getWeekList(date, new OnLoadWeekListener() {

			@Override
			public void onError(String msg) {
				ProgressUtil.hideLoading(mProgressDialog, MainActivity.this);
			}

			@Override
			public void onComplete(Week week) {
				WeekPagerAdapter adapter = new WeekPagerAdapter(getSupportFragmentManager(), getApplicationContext(),
						week);
				mPager.setAdapter(adapter);
				mIndicator.setViewPager(mPager);
				ProgressUtil.hideLoading(mProgressDialog, MainActivity.this);

			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		if (item.getTitle().equals(getString(R.string.action_settings))) {
			Intent settingsIntent = new Intent(this, SettingsActivity.class);
			startActivity(settingsIntent);
		}

		if (item.getTitle().equals(getString(R.string.action_refresh))) {
			initFragments();
		}

		if (item.getTitle().equals(getString(R.string.action_select_week))) {
			showSelectWeekFragment();
		}

		return super.onMenuItemSelected(featureId, item);
	}

	private void showSelectWeekFragment() {
		SelectWeekFragment fragment = new SelectWeekFragment(new OnDateSelectListener() {

			@Override
			public void onDateSelected(Date date) {
				mCurrentWeek = date;
				initFragments(date);
			}
		});
		fragment.show(getSupportFragmentManager(), TAG);
	}

}
